//
//  XFWelcomeViewController.swift
//  mxfweibo
//
//  Created by apple1 on 15/12/17.
//  Copyright © 2015年 莫祥富. All rights reserved.
//

import UIKit

import SDWebImage

class XFWelcomeViewController: UIViewController {

    private var iconViewBtnConstraint : NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        
        setIconView()
        
        XFUserAccount.loadUserAccount()?.loadUserInfo({ (error) -> () in
            if error != nil {
                print("获取用户信息出错了")
                return
            }
            self.setIconView()
        })
    }
    
    
    private func setIconView() {
        
        if let avatar_large = XFUserAccount.loadUserAccount()?.avatar_large {
            
            let url = NSURL(string: avatar_large)!
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconViewBtnConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                self.welcomeLabel.alpha = 0
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    self.welcomeLabel.hidden = false
                    }, completion: { (_) -> Void in
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
//                        AppDelegate.outSwitchRootViewController(true)
                })
        }
   
    }
    

    //MARK : - 准备UI
    private func prepareUI()
    {
        view.addSubview(bgImgView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
//        bgImgView.translatesAutoresizingMaskIntoConstraints = false
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
//        
//        view.addConstraint(NSLayoutConstraint(item: bgImgView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bgImgView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bgImgView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bgImgView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
//        
//        
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        iconViewBtnConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
//        view.addConstraint(iconViewBtnConstraint!)
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
//        
        bgImgView.ff_Fill(view)
        
        let constraint = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        iconViewBtnConstraint = iconView.ff_Constraint(constraint, attribute: NSLayoutAttribute.Bottom)
        
        welcomeLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
    }

 
    //MARK: - 数据懒加载
    private lazy var bgImgView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView : UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imgView.layer.cornerRadius = 42.5
        imgView.layer.masksToBounds = true
        return imgView
    }()
    
    private lazy var welcomeLabel : UILabel = {
       
        let label = UILabel()
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(14)
        
        label.hidden = true
        return label
    }()
    
    

}
